package org.sky.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysDict;
import org.sky.sys.entity.SysDictItem;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.mapper.SysDictItemMapper;
import org.sky.sys.mapper.SysDictMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author weifx
 * @Title: SysDictService
 * @ProjectName springboot-platform
 * @Description: TODO{字典业务类}
 * @date 2019/3/12 22:10
 * @Version 1.0
 */
@Service
public class SysDictService {
    private static final Logger log = LogUtils.getLogger(SysDictService.class);
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;

    /**
     * {字典类型分页查询}
     * @param pageNum
     * @param pageSize
     * @param example
     * @return
     */
    public PageListData selectSysDictByPage(int pageNum, int pageSize, Example example){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<SysDict> pageInfo = new PageInfo<SysDict>(sysDictMapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }

    /**
     * 字典项分页查询
     * @param pageNum
     * @param pageSize
     * @param example
     * @return
     */
    public PageListData selectSysDictItemByPage(int pageNum, int pageSize, Example example){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<SysDictItem> pageInfo = new PageInfo<SysDictItem>(sysDictItemMapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }

    /**
     * 根据主键查询字典类型
     * @param id
     * @return
     */
    public SysDict getSysDictById(Long id){
        Example example = new Example(SysDict.class);
        example.createCriteria().andEqualTo("id",id);
        return sysDictMapper.selectOneByExample(example);
    }

    /**
     * 根据主键查询字典项
     * @param id
     * @return
     */
    public SysDictItem getSysDictItemById(Long id){
        Example example = new Example(SysDictItem.class);
        example.createCriteria().andEqualTo("id",id);
        return sysDictItemMapper.selectOneByExample(example);
    }

    /**
     * 字典类型删除
     * @param list
     * @throws ServiceException
     */
    @Transactional
    public void delSysDictById(List<SysDict> list) throws ServiceException{
        Example example = new Example(SysDict.class);
        Example eitem = new Example(SysDictItem.class);
        try {
            for (SysDict sd : list) {
                example.clear();
                example.createCriteria().andEqualTo("id", sd.getId());
                sysDictMapper.deleteByExample(example);
                eitem.clear();
                eitem.createCriteria().andEqualTo("dictCode",sd.getCode());
                sysDictItemMapper.deleteByExample(eitem);
            }
        }catch (Exception e){
            log.error("字典类型删除失败:{}",e.getMessage());
            e.printStackTrace();
            throw new ServiceException("字典类型删除失败:"+e.getMessage(),e);
        }
    }

    /**
     * 字典项删除
     * @param list
     * @throws ServiceException
     */
    public void delSysDictItemById(List<SysDictItem> list) throws ServiceException{
        Example example = new Example(SysDictItem.class);
        try {
            for (SysDictItem sdi : list) {
                example.clear();
                example.createCriteria().andEqualTo("id", sdi.getId());
                sysDictItemMapper.deleteByExample(example);
            }
        }catch (Exception e){
            log.error("字典项删除失败:{}",e.getMessage());
            e.printStackTrace();
            throw new ServiceException("字典项删除失败",e);
        }
    }

    /**
     * 保存{新增/修改}字典类型
     * @param sysDict
     * @throws ServiceException
     */
    public void saveAddEditSysDict(SysDict sysDict) throws ServiceException{
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null != sysDict.getId()) {
                Example example = new Example(SysDict.class);
                example.createCriteria().andEqualTo("id", sysDict.getId());
                sysDict.setUpdater(BspUtils.getLoginUser().getCode());
                sysDict.setUpdateTime(ts);
                sysDictMapper.updateByExampleSelective(sysDict, example);
            } else {
                sysDict.setId(SnowflakeIdWorker.getInstance().nextId());
                sysDict.setCreater(BspUtils.getLoginUser().getCode());
                sysDict.setCreateTime(ts);
                sysDict.setUpdater(BspUtils.getLoginUser().getCode());
                sysDict.setUpdateTime(ts);
                sysDictMapper.insertSelective(sysDict);
            }
        }catch (Exception e){
            log.error("保存失败:{}",e.getMessage());
            e.printStackTrace();
            throw new ServiceException("保存失败",e);
        }
    }

    /**
     * 保存{新增/修改}字典项
     * @param sysDictItem
     * @throws ServiceException
     */
    public void saveAddEditSysDictItem(SysDictItem sysDictItem) throws ServiceException{
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null != sysDictItem.getId()) {
                Example example = new Example(SysDictItem.class);
                example.createCriteria().andEqualTo("id", sysDictItem.getId());
                sysDictItem.setUpdater(BspUtils.getLoginUser().getCode());
                sysDictItem.setUpdateTime(ts);
                sysDictItemMapper.updateByExampleSelective(sysDictItem, example);
            } else {
                sysDictItem.setId(SnowflakeIdWorker.getInstance().nextId());
                sysDictItem.setCreater(BspUtils.getLoginUser().getCode());
                sysDictItem.setCreateTime(ts);
                sysDictItem.setUpdater(BspUtils.getLoginUser().getCode());
                sysDictItem.setUpdateTime(ts);
                sysDictItemMapper.insertSelective(sysDictItem);
            }
        }catch (Exception e){
            log.error("保存失败:{}",e.getMessage());
            e.printStackTrace();
            throw new ServiceException("保存失败",e);
        }
    }
}
